package com.xzc.shopping.controller;

import com.xzc.shopping.common.BaseResponse;
import com.xzc.shopping.common.ErrorCode;
import com.xzc.shopping.common.ResultUtils;
import com.xzc.shopping.model.result.ThumbResult;
import com.xzc.shopping.model.domain.User;
import com.xzc.shopping.model.request.ThumbRequest;
import com.xzc.shopping.service.ThumbService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.xzc.shopping.constant.UserConstant.USER_LOGIN_STATE;

@RestController
@RequestMapping("/thumb")
@Slf4j
public class ThumbController {
    @Resource
    private ThumbService thumbService;

    // 点赞/取消点赞切换接口
    @PostMapping("/toggle")
    public BaseResponse<ThumbResult> toggleThumb(
            @RequestBody ThumbRequest request, // 接收 JSON 请求体
            HttpServletRequest httpRequest
    ) {
        Integer productId = request.getProductId();
        if(productId == null) {
            return ResultUtils.error(ErrorCode.NULL_ERROR);
        }
        User user = (User) httpRequest.getSession().getAttribute(USER_LOGIN_STATE);
        if(user == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN);
        }
        Integer userId = user.getId();
        return thumbService.toggleThumb(userId,productId);
    }
}
